// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   check_ok.java

package design.price_change;

import include.nseer_cookie.counter;
import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataTag;

public class check_ok extends HttpServlet
{

	public check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 design_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 design_db1 = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 manufacture_db1 = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		ValidataTag vt = new ValidataTag();
		counter count = new counter(dbApplication);
		try
		{
			if (design_db.conn((String)dbSession.getAttribute("unit_db_name")) && manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")) && design_db1.conn((String)dbSession.getAttribute("unit_db_name")) && manufacture_db1.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String checker_ID = request.getParameter("checker_ID");
				String config_id = request.getParameter("config_id");
				String product_ID = request.getParameter("product_ID");
				String list_price2 = request.getParameter("list_price");
				String id = request.getParameter("id");
				String responsible_person_ID = "";
				String responsible_person_name = "";
				String product_name = request.getParameter("product_name");
				String factory_name = request.getParameter("factory_name");
				String type = request.getParameter("type");
				String product_class = request.getParameter("product_class");
				String product_nick = request.getParameter("product_nick");
				String twin_name = request.getParameter("twin_name");
				String twin_ID = request.getParameter("twin_ID");
				String personal_unit = request.getParameter("personal_unit");
				String personal_value = request.getParameter("personal_value");
				String warranty = request.getParameter("warranty");
				String lifecycle = request.getParameter("lifecycle");
				String amount_unit = request.getParameter("amount_unit");
				String register = request.getParameter("register");
				String bodya = new String(request.getParameter("product_describe").getBytes("UTF-8"), "UTF-8");
				String product_describe = exchange.toHtml(bodya);
				String checker = request.getParameter("checker");
				String check_time = request.getParameter("check_time");
				StringTokenizer tokenTO4 = new StringTokenizer(list_price2, ",");
				String list_price;
				String list_price1;
				for (list_price = ""; tokenTO4.hasMoreTokens(); list_price = (new StringBuilder(String.valueOf(list_price))).append(list_price1).toString())
					list_price1 = tokenTO4.nextToken();

				String cost_price2 = request.getParameter("cost_price");
				StringTokenizer tokenTO5 = new StringTokenizer(cost_price2, ",");
				String cost_price;
				String cost_price1;
				for (cost_price = ""; tokenTO5.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
					cost_price1 = tokenTO5.nextToken();

				int n = 0;
				if (!validata.validata(list_price) || Double.parseDouble(list_price) < 0.0D)
					n++;
				if (!validata.validata(cost_price) || Double.parseDouble(cost_price) < 0.0D)
					n++;
				if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "design_file", "product_ID", product_ID, "price_change_tag").equals("1"))
				{
					if (n == 0)
					{
						try
						{
							String sql = (new StringBuilder("update design_file set product_ID='")).append(product_ID).append("',product_name='").append(product_name).append("',factory_name='").append(factory_name).append("',product_class='").append(product_class).append("',type='").append(type).append("',twin_name='").append(twin_name).append("',twin_ID='").append(twin_ID).append("',personal_unit='").append(personal_unit).append("',personal_value='").append(personal_value).append("',warranty='").append(warranty).append("',lifecycle='").append(lifecycle).append("',product_nick='").append(product_nick).append("',list_price='").append(list_price).append("',cost_price='").append(cost_price).append("',product_describe='").append(product_describe).append("',responsible_person_name='").append(responsible_person_name).append("',responsible_person_ID='").append(responsible_person_ID).append("',amount_unit='").append(amount_unit).append("',price_alarm_tag='0',checker='").append(checker).append("',check_time='").append(check_time).append("' where product_ID='").append(product_ID).append("' and price_change_tag='1'").toString();
							design_db.executeUpdate(sql);
							sql = (new StringBuilder("update design_workflow set checker='")).append(checker).append("',checker_ID='").append(checker_ID).append("',check_time='").append(check_time).append("',check_tag='1' where  object_ID='").append(product_ID).append("' and config_id='").append(config_id).append("' and type_id='02'").toString();
							design_db.executeUpdate(sql);
							sql = (new StringBuilder("select id from design_workflow where object_ID='")).append(product_ID).append("' and check_tag='0' and type_id='02'").toString();
							ResultSet rset = design_db.executeQuery(sql);
							if (!rset.next())
							{
								sql = (new StringBuilder("update design_file set price_change_tag='0' where product_ID='")).append(product_ID).append("'").toString();
								design_db.executeUpdate(sql);
								sql = (new StringBuilder("delete from design_workflow where object_ID='")).append(product_ID).append("' and type_id='02'").toString();
								design_db.executeUpdate(sql);
								String sql4 = (new StringBuilder("select * from design_module_details where product_ID='")).append(product_ID).append("'").toString();
								String sql1;
								for (ResultSet rs4 = design_db.executeQuery(sql4); rs4.next(); design_db1.executeUpdate(sql1))
								{
									double subtotal1 = Double.parseDouble(cost_price) * rs4.getDouble("amount");
									double subtotal3 = subtotal1 - rs4.getDouble("subtotal");
									String sql6 = (new StringBuilder("select * from design_module where design_ID='")).append(rs4.getString("design_ID")).append("'").toString();
									ResultSet rs6 = design_db1.executeQuery(sql6);
									if (rs6.next())
									{
										double subtotal5 = rs6.getDouble("cost_price_sum") + subtotal3;
										String sql8 = (new StringBuilder("update design_module set cost_price_sum='")).append(subtotal5).append("' where design_ID='").append(rs4.getString("design_ID")).append("'").toString();
										design_db1.executeUpdate(sql8);
									}
									sql1 = (new StringBuilder("update design_module_details set cost_price='")).append(cost_price).append("',subtotal='").append(subtotal1).append("' where product_ID='").append(product_ID).append("'").toString();
								}

								String sql5 = (new StringBuilder("select * from manufacture_design_procedure_module_details where product_ID='")).append(product_ID).append("'").toString();
								String sql3;
								for (ResultSet rs5 = manufacture_db.executeQuery(sql5); rs5.next(); manufacture_db1.executeUpdate(sql3))
								{
									double subtotal2 = Double.parseDouble(cost_price) * rs5.getDouble("amount");
									double subtotal4 = subtotal2 - rs5.getDouble("subtotal");
									String sql7 = (new StringBuilder("select * from manufacture_design_procedure_details where design_ID='")).append(rs5.getString("design_ID")).append("' and procedure_name='").append(rs5.getString("procedure_name")).append("'").toString();
									ResultSet rs7 = manufacture_db1.executeQuery(sql7);
									if (rs7.next())
									{
										double subtotal6 = rs7.getDouble("module_subtotal") + subtotal4;
										String sql9 = (new StringBuilder("update manufacture_design_procedure_details set module_subtotal='")).append(subtotal6).append("' where design_ID='").append(rs5.getString("design_ID")).append("' and procedure_name='").append(rs5.getString("procedure_name")).append("'").toString();
										manufacture_db1.executeUpdate(sql9);
									}
									String sql11 = (new StringBuilder("select * from manufacture_design_procedure where design_ID='")).append(rs5.getString("design_ID")).append("'").toString();
									ResultSet rs11 = manufacture_db1.executeQuery(sql11);
									if (rs11.next())
									{
										double subtotal8 = rs11.getDouble("module_cost_price_sum") + subtotal4;
										String sql13 = (new StringBuilder("update manufacture_design_procedure set module_cost_price_sum='")).append(subtotal8).append("' where design_ID='").append(rs5.getString("design_ID")).append("'").toString();
										manufacture_db1.executeUpdate(sql13);
									}
									sql3 = (new StringBuilder("update manufacture_design_procedure_module_details set cost_price='")).append(cost_price).append("',subtotal='").append(subtotal2).append("' where product_ID='").append(product_ID).append("'").toString();
								}

							}
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect("design/price_change/check_ok_a.jsp");
					} else
					{
						response.sendRedirect((new StringBuilder("design/price_change/check_ok_c.jsp?product_ID=")).append(product_ID).toString());
					}
				} else
				{
					response.sendRedirect("design/price_change/check_ok_d.jsp");
				}
				design_db.commit();
				design_db1.commit();
				manufacture_db1.commit();
				manufacture_db.commit();
				design_db.close();
				design_db1.close();
				manufacture_db1.close();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
